Skip to content

Commit

Permalink
Small improvement to "coerce2" method for DataFrame objects
Browse files Browse the repository at this point in the history
  • Loading branch information
hpages committed Nov 14, 2018
1 parent 606a89b commit 48e11dd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -8,7 +8,7 @@ Description: The S4Vectors package defines the Vector and List virtual classes
interest (e.g. DataFrame, Rle, and Hits) are implemented in the
S4Vectors package itself (many more are implemented in the IRanges
package and in other Bioconductor infrastructure packages).
Version: 0.21.4
Version: 0.21.5
Encoding: UTF-8
Author: H. Pagès, M. Lawrence and P. Aboyoun
Maintainer: Bioconductor Package Maintainer <maintainer@bioconductor.org>
Expand Down
18 changes: 16 additions & 2 deletions R/DataFrame-class.R
Expand Up @@ -763,17 +763,31 @@ setAs("ANY", "DataTable_OR_NULL", function(from) as(from, "DataFrame"))
setMethod("coerce2", "DataFrame",
function(from, to)
{
to_class <- class(to)
if (class(from) == "list") {
## Turn an ordinary list into a DataFrame in the most possibly
## straightforward way.
return(new_DataFrame(from, what="list elements"))
ans <- new_DataFrame(from, what="list elements")
if (is(ans, to_class))
return(ans)
ans <- as(ans, to_class, strict=FALSE)
## Even though coercion from DataFrame to 'class(to)' "worked", it
## can return a broken object. This happens when an automatic
## coercion method gets in the way. The problem with these methods
## is that they often do the wrong thing and don't even bother to
## validate the object they return!
## One possible problem with an automatic coercion method from
## DataFrame to a DataFrame subclass is that it will set the
## elementType slot to "ANY" which could be wrong. So we fix this.
ans@elementType <- to@elementType
validObject(ans)
return(ans)
}
## Some objects like SplitDataFrameList have a "dim" method that
## returns a non-MULL object (a matrix!) even though they don't have
## an array-like (or matrix-like) semantic.
from_dim <- dim(from)
if (length(from_dim) == 2L && !is.matrix(from_dim)) {
to_class <- class(to)
if (is(from, to_class))
return(from)
ans <- as(from, to_class, strict=FALSE)
Expand Down
4 changes: 2 additions & 2 deletions R/SimpleList-class.R
Expand Up @@ -186,8 +186,8 @@ setMethod("coerce2", "SimpleList",
## often do the wrong thing and don't even bother to validate the
## object they return!
## One known problem with the automatic coercion method from SimpleList
## to one of its subclass is that it sets the elementType slot to "ANY"
## which is generally wrong. So we fix this.
## to one of its subclass is that it will set the elementType slot to
## "ANY" which will be wrong in general. So we fix this.
ans@elementType <- to@elementType
validObject(ans)
ans
Expand Down

0 comments on commit 48e11dd

Please sign in to comment.